# -*- coding: utf-8 -*-
# HTTP
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.context_processors import csrf
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login ,logout
# UTIL
import time, string


def user_login(request):
    template_var={}
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password1']
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            return HttpResponseRedirect("/defence/myAlarm/") 
        else:
            template_var["error"]=u"username or password false."
            return render_to_response("account/login.html",template_var,context_instance=RequestContext(request))
    else:
        return render_to_response("account/login.html",template_var,context_instance=RequestContext(request))

def user_register(request):
    template_var={}
    if request.method == "POST":
        try:
            user = User.objects.get(name = request.POST['username'])
            template_var["error"]=u"username already exists, please use another username!"
            return render_to_response("account/register.html",template_var,context_instance=RequestContext(request))
        except: 
            username = request.POST.get('username',None)
            password1 = request.POST.get('password1',None)
            password2 = request.POST.get('password2',None)
            email = request.POST.get('email',None)
            if username is None or password1 is None or password2 is None or email is None:
                template_var["error"]=u"please fill out all the information!"
                return render_to_response("account/register.html",template_var,context_instance=RequestContext(request))
            if password1 != password2 :
                template_var["error"]=u"two password are not same!"
                return render_to_response("account/register.html",template_var,context_instance=RequestContext(request))
            user = User.objects.create_user(username=username, email=email, password=password1)
            if user is not None: 
                return HttpResponseRedirect("/account/login/") 
            else:
                template_var["error"]=u"register fails，please register again!"
                return render_to_response("account/register.html",template_var,context_instance=RequestContext(request))
    else:
        return render_to_response("account/register.html",template_var,context_instance=RequestContext(request))

def user_logout(request):
    logout(request)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

def profile(request):
    template_var={}
    user = request.user
    if request.method == "POST":
        email = request.POST.get("email",None)
        password = request.POST.get("password",None)
        password1 = request.POST.get("password1",None)
        password2 = request.POST.get("password2",None)
        #change email
        if email is None :
            template_var["emailMsg"]=u"email address missed!"
        elif email != "" and email != user.email:
            try:
                user.email = email
                user.save()
                template_var["emailMsg"]=u"email changes successfully!"
            except Exception, e:
                template_var["emailMsg"]=u"email fails to change!"
        #change password 
        if password is None or password1 is None or password2 is None:
            template_var["passwordMsg"]=u"password missed!"
        elif password == "":
            pass
        elif not user.check_password(password):
            template_var["passwordMsg"]=u"password incorrect!"
        elif password1 !="" and password2 !="" and password1 == password2:
            try:
                user.set_password(password1)
                user.save()
                template_var["passwordMsg"]=u"password changes successfully!"
            except Exception, e:
                template_var["passwordMsg"]=u"password fails to change!"
        else:
            template_var["passwordMsg"]=u"new password incorrect!" 
    return render_to_response('account/profile.html',template_var,context_instance=RequestContext(request))

